<template>
  <div class="app-container">
    <label class="title" style="font-size: 20px;color: rgba(0,0,0,.85);font-weight: 600;line-height: 32px">批量修改成绩</label>
    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['business:result:add']"
        >添加</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="resultList" @selection-change="handleSelectionChange">
      <el-table-column label="序号" type="index" align="center">
        <template slot-scope="scope">
          <span>{{scope.$index+(queryParams.pageNo-1)*queryParams.pageSize+1}}</span>
        </template>
      </el-table-column>
      <el-table-column label="准考号" align="center" prop="zkz" />
      <el-table-column label="姓名" align="center" prop="ksName" />
      <el-table-column label="考点" align="center" prop="pointName" />
      <el-table-column label="考试项目" align="center" prop="examName" />
      <el-table-column label="成绩" align="center" prop="result" />
      <el-table-column label="原因" align="center" prop="reason" />
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['business:result:edit']"
          >查看详情</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNo"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改考生考试成绩[针对单个项目的成绩]对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="考点" prop="pointId">
          <el-select v-model="form.pointId" placeholder="请选择考点" clearable size="small" class="selectWidth">
            <el-option
              v-for="dict in examPlaceIds"
              :key="dict.id"
              :label="dict.name"
              :value="dict.id"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="考试项目" prop="examId">
          <el-select v-model="form.examId" placeholder="请选择考试项目" clearable size="small" class="selectWidth">
            <el-option
              v-for="dict in projectIdList"
              :key="dict.id"
              :label="dict.name"
              :value="dict.id"
            />
          </el-select>
        </el-form-item>
        <el-form-item label="考生" prop="ksId">
          <el-input v-model="form.ksId" placeholder="请选择考生">
            <el-button  slot="append" icon="el-icon-search"  @click="listenShow">选择</el-button>
          </el-input>
        </el-form-item>
        <el-form-item label="成绩" prop="result">
          <el-input v-model="form.result" placeholder="请输入" />
        </el-form-item>
        <el-form-item label="原因" prop="reason">
          <el-input v-model="form.reason" type="textarea" :rows="3" placeholder="请输入" />
        </el-form-item>
        <el-form-item label="密码" prop="password">
          <el-input type="password" v-model="form.password" placeholder="请输入密码" class="selectWidth" show-password/>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>

    <!--查看详情 -->
    <el-dialog :title="查看详情" :visible.sync="openDetail" width="500px" append-to-body>
      <el-form ref="form2" :model="form2"  label-width="80px">
        <el-form-item label="考点" prop="pointName">
          <el-input v-model="form2.pointName" :disabled="true" />
        </el-form-item>
        <el-form-item label="考试项目" prop="examName">
          <el-input v-model="form2.examName" :disabled="true" />
        </el-form-item>
        <el-form-item label="考生" prop="ksName">
          <el-input v-model="form2.ksName" :disabled="true" />
        </el-form-item>
        <el-form-item label="成绩" prop="result">
          <el-input v-model="form2.result" :disabled="true" />
        </el-form-item>
        <el-form-item label="原因" prop="reason">
          <el-input v-model="form2.reason" type="textarea" :rows="3" :disabled="true" />
        </el-form-item>
      </el-form>
    </el-dialog>

    <!-- 选择考生 -->
    <el-dialog :title="dialogTitle" :visible.sync="dialogSync" width="800px" append-to-body>
      <candidateInfo v-on:listenToChildEvent="listenClient"></candidateInfo>
    </el-dialog>
  </div>
</template>

<script>
    import { listResult, getResult, updateResult,batchUpdateResult ,getExamPlaces,getExamProjects} from "@/api/business/batchModifyResult/batchModifyResult";
    import candidateInfo from "@/views/business/candidate/info";
    const isNum = (rule, value, callback) => {
        const result= /^[0-9]*$/;
        if (!result.test(value)) {
            callback(new Error('成绩只能为数字'))
        }else{
            callback()
        }
    };
    export default {
        name: "Result",
        components:{candidateInfo},
        data() {
            return {
                // 遮罩层
                loading: true,
                // 导出遮罩层
                exportLoading: false,
                // 选中数组
                ids: [],
                // 非单个禁用
                single: true,
                // 非多个禁用
                multiple: true,
                // 显示搜索条件
                showSearch: true,
                // 总条数
                total: 0,
                // 考生考试成绩[针对单个项目的成绩]表格数据
                resultList: [],
                // 弹出层标题
                title: "",
                // 是否显示弹出层
                open: false,
                openDetail:false,
                // 查询参数
                queryParams: {
                    pageNo: 1,
                    pageSize: 10,
                },
                // 表单参数
                form: {},
                form2:{},
                // 表单校验
                rules: {
                    ksId: [
                        { required: true, message: "考生必须填写", trigger: "blur" }
                    ],
                    examId: [
                        { required: true, message: "考试项目必须选择", trigger: "blur" }
                    ],
                    pointId: [
                        { required: true, message: "考点必须选择", trigger: "blur" }
                    ],
                    result: [
                        { required: true, message: "成绩必须填写", trigger: "blur" },
                        { validator: isNum,message: "成绩必须是整数", trigger: 'blur' }
                    ],
                    reason: [
                        { required: true, message: "原因必须填写", trigger: "blur" }
                    ],
                    password: [
                        { required: true, message: "密码必须填写", trigger: "blur" }
                    ],

                },
                //考点列表
                examPlaceIds: [],
                //项目列表
                projectIdList:[],
                dialogTitle: '',
                dialogSync: false
            };
        },
        created() {
            this.getList();
            getExamPlaces().then(response => {
                this.examPlaceIds = response.data;
            });
            getExamProjects().then(response => {
                this.projectIdList = response.data;
            });
        },
        methods: {
            /** 查询考生考试成绩[针对单个项目的成绩]列表 */
            getList() {
                this.loading = true;
                listResult(this.queryParams).then(response => {
                    this.resultList = response.data.records;
                    this.total = response.data.totalCount;
                    this.loading = false;
                });
            },
            // 取消按钮
            cancel() {
                this.open = false;
                this.reset();
            },
            // 表单重置
            reset() {
                this.form = {
                    ksId: null,
                    examId: null,
                    pointId: null,
                    result: null,
                    reason: null,
                    password:null
                };
                this.resetForm("form");
            },
            reset2() {
                this.form2 = {
                    zkz: null,
                    ksName: null,
                    pointName: null,
                    examName: null,
                    result: null,
                    remark:null
                };
                this.resetForm("form2");
            },
            /** 搜索按钮操作 */
            handleQuery() {
                this.queryParams.pageNo = 1;
                this.getList();
            },
            /** 重置按钮操作 */
            resetQuery() {
                this.resetForm("queryForm");
                this.handleQuery();
            },
            // 多选框选中数据
            handleSelectionChange(selection) {
                this.ids = selection.map(item => item.id);
                this.single = selection.length!==1;
                this.multiple = !selection.length
            },
            /** 新增按钮操作 */
            handleAdd() {
                this.reset();
                this.open = true;
                this.title = "添加记录";
            },

            /** 提交按钮 */
            submitForm() {
                this.$refs["form"].validate(valid => {
                    if (valid) {
                        const data = this.form;
                        const datas = {data:data,password:this.form.password};
                        updateResult(datas).then(response => {
                            if (response.status === 'success') {
                                this.$modal.msgSuccess('新增成功');
                                this.open = false;
                                this.getList()
                            }else{
                              this.$modal.msgSuccess(response.message)
                            }
                        });
                    }
                });
            },
            /** 删除按钮操作 */
            handleDelete(row) {
                const ids = row.id || this.ids;
                this.$modal.confirm('是否确认删除考生考试成绩[针对单个项目的成绩]编号为"' + ids + '"的数据项？').then(function() {
                    return delResult(ids);
                }).then(() => {
                    this.getList();
                    this.$modal.msgSuccess("删除成功");
                }).catch(() => {});
            },
            /** 查看详情*/
            handleUpdate(row) {
                this.reset2();
                const id = row.id || this.ids;
                getResult(id).then(response => {
                    this.form2 = response.data;
                    this.openDetail = true;
                });
            },
            /** 选择器监听事件 */
            listenClient(data){
                console.log(data);
                this.dialogSync = false;
                this.form.ksId = data.id;
            },
            listenShow(){
                this.dialogSync = true;
            }
        }
    };
</script>
